package mr;

import java.util.ArrayList;

import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.util.Tool;

public abstract class MRHelper<IK, IV, OK, OV, MS extends Writable> extends
		Configured implements Mapper<IK, IV, OK, OV>,
		Reducer<IK, IV, IV, NullWritable>, Tool {

	protected ArrayList<Mapper<IK, IV, OK, OV>> mappers;
	protected Reducer<IK, IV, OV, NullWritable> reducer;
	protected ArrayList<Reducer<IK, IV, OK, OV>> reducers;

	public MRHelper() {
		mappers = new ArrayList<Mapper<IK, IV, OK, OV>>();
		reducer = null;
	}

	public void addMapper(Mapper<IK, IV, OK, OV> mapper) {
		if (mappers == null)
			mappers = new ArrayList<Mapper<IK, IV, OK, OV>>();
		mappers.add(mapper);
	}

	public void addReducer(Reducer<IK, IV, OV, NullWritable> red) {
		reducer = red;
	}

	public Mapper<IK, IV, OK, OV> getMapper(int id) {
		if (mappers == null || id >= mappers.size())
			return null;
		return mappers.get(id);

	}

	public abstract void setAllMappers();

	public abstract void setReducers();

}